﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using StationUpdate.Util;

public class LcdStreenApi
{
    #region 实体类
    private string url;
    public string Url
    {
        get { return url; }
        set
        {
            //容错机制，当用户传入的地址没有以“/”结尾则自动补上
            url = value.EndsWith("/") ? value : value + "/";
        }
    }
    #endregion



    /// <summary>
    /// 更新URL地址委托
    /// </summary>
    public delegate void NextUrlHandler();
    public event NextUrlHandler NextUrlEvent;
    private void NextUrlNotify()
    {
        if (NextUrlEvent != null)
        {
            this.NextUrlEvent();
        }
    }
    private object sysObj = new object();
    #region 私有方法
    /// <summary>
    /// 使用post方法请求WebService
    /// </summary>
    /// <param name="MethodName">方法名</param>
    /// <param name="ParamtersStr">参数串（单参数使用string类型，多参数需以json方式格式化）</param>
    /// <returns></returns>
    private string SendByPost(string MethodName)
    {
        lock (sysObj)
        {
            string urlstr = Url + MethodName;//拼接web服务地址
            WebClient webClient = new WebClient();

            Encoding enc = Encoding.GetEncoding("UTF-8");
            Byte[] pageData = webClient.DownloadData(urlstr);
            return enc.GetString(pageData);
        }
    }
    #endregion
    private string SendByPost(string MethodName, string ParamtersStr)
    {
        byte[] responseData = null;
        lock (sysObj)
        {
            string urlstr = Url + MethodName;//拼接web服务地址
            byte[] postData = Encoding.UTF8.GetBytes(ParamtersStr);//编码，尤其是汉字，事先要看下抓取网页的编码方式   
            WebClient webClient = new WebClient();
            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header，如果改为GET方式的话就去掉这句话即可   
            //webClient.Headers.Add("Authorization", AuthUtil.getAuth());
            
            try
            {
                responseData = webClient.UploadData(urlstr, "POST", postData);//得到返回字符流   
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        return Encoding.UTF8.GetString(responseData);
    }

    private string SendByGet(string MethodName, string content)
    {
        string urlstr = Url + MethodName ;//拼接web服务地址
        WebClient webClient = new WebClient();
        webClient.Headers.Add("Authorization", AuthUtil.getAuth(content));
        Encoding enc = Encoding.GetEncoding("UTF-8");
        Byte[] pageData = webClient.DownloadData(urlstr);
        return enc.GetString(pageData);

    }
    #region 方法

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public string getUpGrade(string lcdNo)
    {
        try
        {
            return SendByGet("api/stopbordApi/getUpgradeByMachine?machineNo=" + lcdNo, "machineNo=" + lcdNo);
            //string para = "machineNo="+lcdNo;    
            //return SendByPost("tiamaes-tmsp-stopbord/api/stopbordApi/getUpgradeByMachine", para);



            //string para = "lcdNo=" + lcdNo;
            //return SendByPost("mdmService/api/config/upGrade", para);
        }
        catch (Exception ex)
        {
            //需要外部处理异常
            throw ex;
        }
    }
    public DateTime GetSysTime()
    {
        DateTime ret = DateTime.Now;
        try
        {

        }
        catch (Exception ex)
        {
            //需要外部处理异常
            throw ex;
        }
        return ret;
    }

    public bool CheckModel(string str)
    {
        try
        {
            return true;
        }
        catch (Exception ex)
        {
            //需要外部处理异常
            throw ex;
        }
    }
    #endregion

}
